package com.goldplusgold.network.lib.http.netty;

import com.goldplusgold.network.lib.http.protobuf.Market;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import io.netty.channel.Channel;

/**
 * Created by Administrator on 2017/4/5.
 */

public class ChannelManager {
    private static Map<Integer, Channel> sessionMap = new ConcurrentHashMap<Integer, Channel>();

    public static void addSession(int playerId, Channel channel) {
        if (sessionMap.containsKey(playerId)) {
            if (sessionMap.get(playerId) == channel) {
                return;
            }
        }
        sessionMap.put(playerId, channel);
    }

    public static void removeSession(int playerId) {
        if (sessionMap.containsKey(playerId)) {
            sessionMap.remove(playerId);
        }
    }

    public static Channel getSession(int playerId) {
        if (!sessionMap.containsKey(playerId)) {
            return null;
        }
        return sessionMap.get(playerId);
    }

    public static void sendMessage(int cmd, Market.Response data, int playerId) {
        if (sessionMap.containsKey(playerId)) {
            sessionMap.get(playerId).writeAndFlush(data);
        }
    }

    public static void close(int playerId) {
        if (sessionMap.containsKey(playerId)) {
            sessionMap.get(playerId).close();
        }
    }
}
